package leetcode.题库.栈;

import java.util.Stack;

/**
 * description:
 *
 * @author 颜真明
 * @date 2024/3/8  12:41
 */
public class Solution20 {
    public static void main(String[] args) {
        System.out.println(isValid("()"));
    }
    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (char c : s.toCharArray()) {
            if (c == '(' || c == '[' || c == '{' || stack.isEmpty()) {
                stack.push(c);
                continue;
            }
            if (c == ')' && stack.peek() != '(') {
                return false;
            }
            if (c == ']' && stack.peek() != '[') {
                return false;
            }
            if (c == '}' && stack.peek() != '{') {
                return false;
            }
            stack.pop();
        }
        return stack.isEmpty();
    }
}
